package com.lee.lease.web.app.controller.lease;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lee.lease.common.result.Result;
import com.lee.lease.common.util.JwtUtil;
import com.lee.lease.model.entity.LeaseAgreement;
import com.lee.lease.model.enums.LeaseStatus;
import com.lee.lease.web.app.mapper.LeaseAgreementMapper;
import com.lee.lease.web.app.service.LeaseAgreementService;
import com.lee.lease.web.app.vo.agreement.AgreementDetailVo;
import com.lee.lease.web.app.vo.agreement.AgreementItemVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtHandler;
import io.minio.credentials.Jwt;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: LeaseController
 * Package: com.lee.lease.web.app.controller.lease
 * Description:
 */
@RestController
@Tag(name = "租约")
@RequestMapping("/app/agreement")
public class LeaseController {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Operation(summary = "根据id更新租约状态")
    @PostMapping("/updateStatusById")
    public Result updateStatusById(Integer id, LeaseStatus leaseStatus) {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LeaseAgreement::getId, id).set(LeaseAgreement::getStatus, leaseStatus);
        leaseAgreementService.update(updateWrapper);
        return Result.ok();
    }

    @Operation(summary = "保存或更新租约")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement){
        leaseAgreementService.saveOrUpdate(leaseAgreement);
        return  Result.ok();
    }
    @Operation(summary = "获取个人租约基本信息列表")
    @GetMapping("listItem")
    public Result<List<AgreementItemVo>> getListItem(@RequestHeader("access_token") String token){
        String phone = JwtUtil.parseToken(token).get("username", String.class);
        List<AgreementItemVo> agreementItemVo = leaseAgreementService.getListItem(phone);
        return Result.ok(agreementItemVo);
    }

    @Operation(summary = "根据id获取租约详情")
    @GetMapping("/getDetailById")
    public Result<AgreementDetailVo> getDetailById(Integer id){
        AgreementDetailVo agreementDetailVo = leaseAgreementService.getDetailById(id);
        return  Result.ok(agreementDetailVo);
    }
}
